<?php
/**
 * Created by PhpStorm.
 * User: LiuJiaJun
 * Date: 2018/12/15
 * Time: 15:09
 */

namespace service\k3\order;


use service\k3\Reader;
use think\Db;

abstract class AbstractOrder implements Reader
{

    /**
     * 出库单类型
     *
     * @var string
     */
    protected $type = '';

    /**
     * 所对应的视图
     * @var string
     */
    protected $table = '';

    /**
     * 查询的字段
     * @var string
     */
    protected $field = '';

    /**
     * 查询视图的sql 语句
     * @var string
     */
    protected $parseSql = '';

    public function __construct()
    {
        $this->_initialize();
    }

    protected function _initialize()
    {
        $this->setType();
        $this->setTable();
        $this->setFiled();
        $this->setParseSql();
    }

    /**
     * 设置类型
     *
     * @return mixed
     */
    abstract function setType();

    /**
     * 设置视图名字
     *
     * @return mixed
     */
    abstract function setTable();

    /**
     * 设置所查询的字段
     *
     * @return mixed
     */
    abstract function setFiled();

    /**
     * 设置解析的sql语句
     *
     * @return mixed
     */
    abstract function setParseSql();

    /**
     * 读取数据
     *
     * @param string $condition
     * @return mixed
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function read($condition = '')
    {
        if (is_array($condition)) {
            $condition = array_values($condition);
            $sql = sprintf($this->parseSql, $condition[0], $condition[1]);
        } else {
            $sql = sprintf($this->parseSql, $condition);
        }
        return Db::connect('sqlsrv://sql_erp:ERP0.123#@10.11.8.2:1433/AIS2018PRTTECH#utf8')->query($sql);
       // return Db::connect('k3_config')->query($sql);
        //TODO::配置文件读取问题
    }
}